/**
 * Created by dbwyxl on 2017/9/11.
 */
module.exports =function($cookies,$scope, $rootScope,$timeout,$state,caseSourcesService){

  //获取分类列表
  $scope.getCaseSourceList=getCaseSourceList;
  //获取案源
  $scope.getServiceList=getServiceList;
  //分页
  $scope.getServiceListPag=getServiceListPag;
  //筛选列表
  $scope.getBusinessType=getBusinessType;
  $scope.addBusinessList=addBusinessList;
  $scope.addBusinessListAll=addBusinessListAll;
  $scope.addBusinessSubmit=addBusinessSubmit;
  $scope.addBusinessCancel=addBusinessCancel;
  $scope.caseSouConClassifyToggle=caseSouConClassifyToggle;
  //筛选功能
  $scope.businessScreen=businessScreen;
  $scope.businessScreenAll=businessScreenAll;
  $scope.businessAnswerAll=businessAnswerAll;

  ///跳转详情
  $scope.skipBillDetails=skipBillDetails;
  //左侧分类列表
  $scope.caseSourceList=[];
  $scope.serviceType=1;
  //筛选选项
  $scope.businessList=JSON.parse(sessionStorage.getItem('businessList'))||[];
  $scope.businessScreenCon='';
  $scope.businessAnswerScreen='';
  //更新时间
  $scope.refreshDate=refreshDate;
  $scope.refreshDateC=refreshDateC;
  //确认接单
  $scope.caseBillAff=caseBillAff;

  $scope.init=function(){
    //初始化

    //获取左侧列表
    getCaseSourceList();
    $scope.refreshDate();
  };
  $scope.init();

  function getCaseSourceList(){
    caseSourcesService.getServiceType().then(function(data){
      if(data.code==1){
        $scope.caseSourceList=data.data;
        //获取分组
        getBusinessType()
        //获取案源
        getServiceList(1);
      }
    })
  }
  function getServiceList(servicetype){
    $scope.pageSetting={
      'servicetype':servicetype,
      'businesstypes':[],
      'pageNum':1,
      'pageSize':10
    };
    getServiceListPag();
  }

  function getServiceListPag(){
    var newCaseSources=$scope.pageSetting;
    caseSourcesService.getServiceList(newCaseSources).then(function(data){
      if(data.code==1){
        $scope.caseSourceContent=data.data.list;
        $scope.serviceType=newCaseSources.servicetype;
        $scope.pageSetting.totalItems=data.data.totalCount;
      }
    })
  }

  function getBusinessType(){
    caseSourcesService.getBusinessType().then(function(data){
      if(data.data.code==1){
        $scope.businessListClassify=data.data.data;
      }
    })
  }
  function addBusinessList(checkBox){
    if(checkBox.status==0){
      checkBox.status=1
    }else{
      checkBox.status=0
    }
    var checkBoxNum=0;
    for(var i=0;i<$scope.businessListClassify.length;i++){
      if($scope.businessListClassify[i].status==0){
        checkBoxNum++;
      }else{
        checkBoxNum--;
      }
    }
    if(checkBoxNum==$scope.businessListClassify.length){
      $scope.AllBoxActive=true;
    }else{
      $scope.AllBoxActive=false;
    }
  }
  function addBusinessSubmit(){
    $scope.businessList=[];
    for(var i=0;i<$scope.businessListClassify.length;i++){
      if($scope.businessListClassify[i].status==0){
        $scope.businessList.push($scope.businessListClassify[i])
      }
    };
    var businessListA=$scope.businessList;
    sessionStorage.setItem('businessList',JSON.stringify(businessListA));
    $scope.caseSouConClassify=false;
  }

  function addBusinessListAll (checkBoxAll){
    $scope.thisStatus=!$scope.thisStatus;
    for(var i=0;i<checkBoxAll.length;i++){
      checkBoxAll[i].status=Number(!$scope.thisStatus);
    }
    $scope.AllBoxActive=$scope.thisStatus;
  }

  function addBusinessCancel(){
    var businessArray = [];
    for (var i = 0;i<$scope.businessList.length;i++) {
      businessArray.push($scope.businessList[i].id);
    }
    for(var i=0;i<$scope.businessListClassify.length;i++){
        if($.inArray($scope.businessListClassify[i].id, businessArray)> -1){
          $scope.businessListClassify[i].status=0;
        }else{
          $scope.businessListClassify[i].status=1;

        }
    }
    if($scope.businessList.length==$scope.businessListClassify.length){
      $scope.AllBoxActive=true;
    }else{
      $scope.thisStatus=false;
      $scope.AllBoxActive=false;
    }
    $scope.caseSouConClassify=false;
  }

  function caseSouConClassifyToggle(){
    var businessArray = [];
    for (var i = 0;i<$scope.businessList.length;i++) {
      businessArray.push($scope.businessList[i].id);
    }
    for(var i=0;i<$scope.businessListClassify.length;i++){
      if($.inArray($scope.businessListClassify[i].id, businessArray)> -1){
        $scope.businessListClassify[i].status=0;
      }else{
        $scope.businessListClassify[i].status=1;
      }
    }
    if($scope.businessList.length==$scope.businessListClassify.length){
      $scope.AllBoxActive=true;
    }else{
      $scope.AllBoxActive=false;
    }
    $scope.caseSouConClassify=true;
  }

  //筛选功能
  function businessScreen (screen){
    $scope.businessScreenCon=screen;
  }
  function businessScreenAll(){
    $scope.businessScreenCon='';
  }
  function businessAnswerAll(screen){
    if(screen!=undefined){
      $scope.businessAnswerScreen=screen;
    }else{
      $scope.businessAnswerScreen='';
    }

  }
  function skipBillDetails(BillId,serviceid){
    sessionStorage.setItem('businessListD',JSON.stringify($scope.businessListClassify));
    $state.go('caseSourcesDetails',{billId:BillId,serviceid:serviceid})

  }
  function caseBillAff(billId,event){
    bootbox.dialog({
      message: "确定要接单？",
      title: "提示信息",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",
          callback: function () {
            caseSourcesService.takeService(billId).then(function(data){
              if(data.code==1){
                $state.go('orderReceivingDetails',{id:data.id})
              }
            });
          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          callback: function () {
          }
        }
      }
    });

    event.stopPropagation();

  }
  //刷新时间
  function refreshDate(){
    $scope.refreshDateS=false;
    var date=new Date();
    $scope.refreshDateT=date.getHours()+':'+date.getMinutes();
    $timeout(function(){
      $scope.refreshDateS=true;
    },60000*5)
  }
  function refreshDateC (){
    getServiceListPag()
    refreshDate()
  }
};
